
package se.hh.lab.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import se.hh.lab.dao.DaoException;
import se.hh.lab.dao.RecordDao;
import se.hh.lab.domain.Record;
import se.hh.lab.jdbc.JdbcUtil;

public class RecordDaoImpl implements RecordDao
{

	@SuppressWarnings("finally")
	@Override
	public int updateRecord(Record re) 
	{
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs =null;		
		int mark=0;
		try
		{
			con = JdbcUtil.getConnection();
			String sql="update lab_record set userID=?,barcode=?,sbackDate=?,backDate=? where recordID=? " ;
			ps = con.prepareStatement(sql);	
			ps.setString(1,re.getUserID());
			ps.setString(2,re.getBarcode());
			ps.setDate(3,re.getSbackdate());
			ps.setDate(4,re.getBackdate());
			ps.setInt(3, re.getRecordID());
			mark=ps.executeUpdate();			
		}	
		catch (Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}

		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
			return mark;
		}			
	}

	public ArrayList<Record> getRecord(Record re,int page) 
	{

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs =null;		
		String sql=null;		
		Integer pagesize=5;
		page-=1;
		Integer pagestart=page*pagesize;
		ArrayList<Record> list=new ArrayList<Record>();
		try
		{				
			if(re.getUserID()!=null && re.getBarcode()!=null )
			{
				sql="select recordID,barcode,userID,borrowDate,sbackDate,backDate from lab_record" +
						" where userID='"+re.getUserID()+"' and barcode='"+re.getBarcode()+"' limit "+pagestart+","+pagesize+" ";
			}
			else if(re.getUserID()!=null)
			{
				sql="select recordID,barcode,userID,borrowDate,sbackDate,backDate from lab_record" +
						" where userID='"+re.getUserID()+"' limit "+pagestart+","+pagesize+" ";
			}
			else if( re.getBarcode()!=null )
			{
				sql="select recordID,barcode,userID,borrowDate,sbackDate,backDate from lab_record" +
						" where  barcode='"+re.getBarcode()+"' limit "+pagestart+","+pagesize+"";
			}
			
			con = JdbcUtil.getConnection();						
			ps = con.prepareStatement(sql);
			rs=ps.executeQuery();
			while (rs.next())
			{

				re=new Record();
				re.setRecordID(rs.getInt("recordID"));
				re.setUserID(rs.getString("userID"));
				re.setBarcode(rs.getString("barcode"));
				re.setBorrowdate(rs.getDate("borrowDate"));
				re.setBackdate(rs.getDate("backDate"));
				re.setSbackdate(rs.getDate("sbackDate"));
				list.add(re);
			}
		}	
		catch (Exception e)
		{
			throw new DaoException(e.getMessage(),e);
		}

		finally
		{
			try
			{
				JdbcUtil.free(rs, ps, con);
			}
			catch (SQLException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}				
		}			
		return list;		
	}

}
